package com.clubs.coding.Service.Impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.clubs.coding.Service.AthAdminUserService;
import com.clubs.coding.exception.ImoocMallException;
import com.clubs.coding.exception.ImoocMallExceptionEnum;
import com.clubs.coding.model.Vo.MenusVo;
import com.clubs.coding.model.dao.AthAdminUserMapper;
import com.clubs.coding.model.dao.MenusMapper;
import com.clubs.coding.model.dao.UserMapper;
import com.clubs.coding.model.pojo.AthAdminUser;
import com.clubs.coding.model.pojo.User;
import com.clubs.coding.model.pojo.menus;
import com.clubs.coding.utils.MD5Utils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;
import java.util.List;

@Service
public class AthAdminUserServiceImpl implements AthAdminUserService {
@Autowired
    AthAdminUserMapper athAdminUserMapper;
@Autowired
    MenusMapper menusMapper;
@Autowired
    UserMapper userMapper;
    @Override
    public AthAdminUser login(String userName, String password) throws ImoocMallException {
        String md5Password = null;
        try {
            md5Password = MD5Utils.getMD5Str(password);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        AthAdminUser athAdminUser = athAdminUserMapper.selectLogin(userName, md5Password);
        if (athAdminUser == null) {
            throw new ImoocMallException(ImoocMallExceptionEnum.WRONG_PASSWORD);
        }
        return athAdminUser;
    }
    @Override
    public void alter(String userName,String passwordold, String password) throws ImoocMallException {
        String md5Password = null;
        String md5Passwordold = null;
        try {
            md5Passwordold = MD5Utils.getMD5Str(passwordold);
            md5Password = MD5Utils.getMD5Str(password);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        AthAdminUser athAdminUserorder = athAdminUserMapper.selectLogin(userName, md5Passwordold);

        if (athAdminUserorder == null) {
            throw new ImoocMallException(ImoocMallExceptionEnum.WRONG_PASSWORD);
        }
        athAdminUserorder.setPassWord(md5Password);
        int i= athAdminUserMapper.updateByPrimaryKeySelective(athAdminUserorder);

    }
/**
 * 会员与游客列表
 * **/
@Override
 public PageInfo member( Integer PageNum, Integer PageSize
         ,Integer member,Integer id,String nickName,Integer mobile){
      PageHelper.startPage(PageNum, PageSize,"create_time desc");//初始化pageInfo
     List<User> users = userMapper.selectAll(member,id,nickName,mobile);


     PageInfo pageInfo = new PageInfo<>(users); //返回一个pageinfo对象

     return pageInfo;
 }
 /**菜单树 **/
 @Override
 public List<Tree<String>> menus(){
     List<MenusVo> nodeList = CollUtil.newArrayList();
     List<menus> menus = menusMapper.selectAll();
     for (int i = 0; i < menus.size(); i++) {
         MenusVo menusVo = new MenusVo();
         com.clubs.coding.model.pojo.menus menus1 =  menus.get(i);
         BeanUtils.copyProperties(menus1, menusVo);
         nodeList.add(menusVo);
     }
     //配置
     TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
     // 自定义属性名 都要默认值的
     treeNodeConfig.setIdKey("id");
     treeNodeConfig.setParentIdKey("parentId");
     treeNodeConfig.setNameKey("name");

     // 最大递归深度
     treeNodeConfig.setDeep(2);
     //转换器
     // 0表示最顶层的id是0
     List<Tree<String>> treeNodes = TreeUtil.build(nodeList, "0", treeNodeConfig,
             (treeNode, tree) -> {
                 tree.setId(treeNode.getMenusId().toString());
                 tree.setParentId(treeNode.getParentid().toString());
                 tree.setName(treeNode.getName());
                 //扩展属性
                 tree.putExtra("sort",treeNode.getSort());
                 tree.putExtra("type",treeNode.getType());
                 tree.putExtra("icon",treeNode.getIcon());
                 tree.putExtra("url",treeNode.getUrl());
             });




     return treeNodes;

 }
/**
 * 修改用户表信息
 *
 * **/
@Override
public void userEd(User user){
    userMapper.updateByPrimaryKeySelective(user);
}

}
